package com.yunji.web.storage;

import com.github.pagehelper.page.PageMethod;
import com.yunji.aggregate.model.dto.stock.StockTakeParam;
import com.yunji.aggregate.model.vo.stock.StockTakeStatisticsVO;
import com.yunji.aggregate.service.StockAggregateService;
import com.yunji.base.enums.AuditStatus;
import com.yunji.common.base.BaseController;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.utils.ExcelUtil;
import com.yunji.storage.business.StockTakeBusiness;
import com.yunji.storage.dto.StockTakeDTO;
import com.yunji.storage.vo.StockTakeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.web.storage
 * @Title: StockTakeController
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-08-30 10:06:36      V1.0        HuaAo       新建类
 */
@Api(tags = "管理端-盘点模块")
@RestController
@RequestMapping("storage/stockTake")
public class StockTakeController extends BaseController {

    @Resource
    StockTakeBusiness business;

    @Resource
    StockAggregateService aggregateService;

    /**
     * @param param
     * @Title export
     * @Desc 导出盘点单
     * @Date 2024-10-10 15:01:30.659
     */
    @ApiOperation("导出盘点单")
    @PostMapping("/export")
//    @PreAuthorize("@ss.hasPermi('inventory:count:export')")
    public void export(StockTakeParam param) {
        List<StockTakeStatisticsVO> list = aggregateService.searchStockTakeList(param);
        ExcelUtil<StockTakeStatisticsVO> util = new ExcelUtil<>(StockTakeStatisticsVO.class);
        util.exportExcel(response, list, "盘点单数据");
    }

    /**
     * @param param 查询参数
     * @Title search
     * @Desc 盘点列表
     * @Date 2024-08-30 15:29:23.158
     * @Return TableDataInfo<RepairTicketVO>
     */
    @ApiOperation("盘点列表")
    @PostMapping("/list")
    public Response<List<StockTakeStatisticsVO>> search(@RequestBody StockTakeParam param) {
        PageMethod.startPage(param.getPageNum(), param.getPageSize());
        List<StockTakeStatisticsVO> list = aggregateService.searchStockTakeList(param);
        return ResponseHelper.successWithPage(list);
    }

    /**
     * 盘点单详情
     *
     * @param stocktakeId
     * @return
     */
    @ApiOperation("盘点单详情")
    @GetMapping("/{stockTakeId}")
    public Response<StockTakeVO> getInfo(@PathVariable Long stockTakeId) {
        return ResponseHelper.success(business.select(stockTakeId));
    }

    /**
     * 新增盘点单
     *
     * @param dto
     * @return
     */
    @ApiOperation("新增盘点单")
    @PostMapping
//    @PreAuthorize("@ss.hasPermi('inventory:count:add')")
    public Response<Long> create(@RequestBody StockTakeDTO dto) {
        return ResponseHelper.success(business.create(dto));
    }

    /**
     * @param dto 录入参数
     * @Title recordOperate
     * @Desc 盘点操作
     * @Date 2024-08-30 16:43:15.262
     * @Return ResponseString>
     */
    @ApiOperation("盘点操作")
    @PutMapping("/operate")
//    @PreAuthorize("@ss.hasPermi('inventory:count:entry')")
    public Response<String> operate(@RequestBody StockTakeDTO dto) {
        business.operate(dto);
        return ResponseHelper.success("操作成功");
    }

    /**
     * 审核通过
     *
     * @param stockTakeId
     * @return
     */
    @ApiOperation("审核通过")
    @GetMapping("/acceptAudit")
    public Response<String> acceptAudit(@RequestParam(name = "stocktakeId") Long stockTakeId) {
        business.audit(stockTakeId, AuditStatus.ACCEPT.getCode(), null);
        return ResponseHelper.success("提交成功");
    }

    /**
     * 审核驳回
     *
     * @param stockTakeId 盘点ID
     * @param reason      驳回原因
     * @return
     */
    @ApiOperation("审核驳回")
    @GetMapping("/rejectAudit")
    public Response<String> rejectAudit(@RequestParam(name = "stocktakeId") Long stockTakeId, @RequestParam(name = "reason") String reason) {
        business.audit(stockTakeId, AuditStatus.REJECT.getCode(), reason);
        return ResponseHelper.success("提交成功");
    }

    /**
     * 取消盘点单
     *
     * @param stockTakeId
     * @return
     */
    @ApiOperation("撤回盘点单")
    @PutMapping("/cancel")
    public Response<String> cancel(@RequestParam Long stocktakeId) {
        business.cancel(stocktakeId);
        return ResponseHelper.success("取消成功");
    }

    /**
     * 重新提交
     *
     * @param dto
     * @return
     */
    @ApiOperation("重新提交")
    @PostMapping("/commit")
    public Response<String> commit(@RequestBody StockTakeDTO dto) {
        business.commit(dto);
        return ResponseHelper.success("提交成功");
    }

    /**
     * 保存草稿
     *
     * @param dto
     * @return
     */
    @ApiOperation("保存草稿")
    @PostMapping("/saveDraft")
    public Response<String> saveDraft(@RequestBody StockTakeDTO dto) {
        business.saveDraft(dto);
        return ResponseHelper.success("新增成功");
    }

    /**
     * 获取草稿
     *
     * @param belongId
     * @return
     */
    @ApiOperation("获取草稿")
    @GetMapping("/getDraft")
    public Response<StockTakeVO> getDraft(@RequestParam Long belongId) {
        return ResponseHelper.success(business.getDraft(belongId));
    }

    /**
     * 批量删除盘点单
     *
     * @return
     */
    @ApiOperation("删除盘点单")
    @DeleteMapping("/{stockTakeIds}")
//    @PreAuthorize("@ss.hasPermi('inventory:count:remove')")
    public Response<String> delete(@PathVariable Long... stockTakeIds) {
        business.delete(stockTakeIds);
        return ResponseHelper.success("删除成功");
    }

}